<?php

namespace Lib\PayApi\Cnzyzf;
use Lib\Utils\Utils;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/22
 * Time: 1:36
 */
class PaymentClient {
    public static function pay($orderNo, $money, $pbank) {
        $cnzyzf = C('CNZYZF');

        $banks = explode('_',$pbank);

        $pay_memberid = $cnzyzf['__PAYNO__'];   //商户ID
        $Md5key = $cnzyzf['__PAYKEY__'];   //商户ID
        $tjurl = $cnzyzf['__PAYDOMAIN__'];   //提交地址

        $pay_amount = $money;    //交易金额
        $pay_applydate = date("Y-m-d H:i:s");  //订单时间
        $pay_bankcode = $banks[0];//"YeePayWx";   //银行编码
        $pay_notifyurl = $cnzyzf["__PAYCALLSERVICE__"];   //异步回掉地址
        $pay_frontendurl = $cnzyzf["__PAYCALLDOMAIN__"];   //服务端返回地址

        $requestarray = array(
            "pay_memberid" => $pay_memberid,
            "pay_orderid" => $orderNo,
            "pay_amount" => $pay_amount,
            "pay_applydate" => $pay_applydate,
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $pay_notifyurl,
            "pay_callbackurl" => $pay_frontendurl
        );

        ksort($requestarray);
        reset($requestarray);

        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str . $key . "=>" . $val . "&";
        }
        $sign = strtoupper(md5($md5str . "key=" . $Md5key));
        $requestarray["pay_md5sign"] = $sign;
        $requestarray["tongdao"] = $cnzyzf['tongdao'][$banks[0]][$banks[1]];
        $requestarray["pay_reserved1"] = $banks[0];

        $str = "";
        $index = 0;
        foreach ($requestarray as $key => $val) {
            if ($index > 0) {
                $str = $str. '&';
            }
            $str = $str. $key . '=' . $val;
            $index++;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $tjurl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $result;
    }
}